package com.binge.backend.service.impl;

import com.binge.backend.dao.UserMapper;
import com.binge.backend.dto.LoginDto;
import com.binge.backend.dto.UserDto;
import com.binge.backend.entity.UserEntity;
import com.binge.backend.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author binge
 * @Description
 * @date 2023年10月01日 上午 8:05
 */
@Service
@Slf4j
@Transactional
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public Boolean register(UserDto userDto) {
        if(isExist(userDto.getUsername())){
            log.info("user is exist");
            return false;
        }
        int result = userMapper.insertUser(userDto);
        if(result < 1){
            log.error("insert user is error.");
            return false;
        }
        return true;
    }

    @Override
    public Boolean isExist(String username) {
        UserEntity userEntity = userMapper.getUserByName(username);
        if(userEntity != null){
            return true;
        }
        return false;
    }

    @Override
    public Boolean login(LoginDto loginDto) {
        UserEntity userEntity = userMapper.getUserByNameAndPwd(loginDto.getUsername(),loginDto.getPassword());
        if(userEntity == null){
            return false;
        }
        return true;
    }
}